
package com.borqs.music.ui.online;

import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_FAILED;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_LOADING;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_NORMAL;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.TYPE_NORMAL;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.TYPE_STATUS;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.borqs.music.database.CacheUtils;
import com.borqs.music.database.CacheUtils.SingerCache;
import com.borqs.music.executor.MusicExecutor;
import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskAdapter;
import com.borqs.music.model.SingerInfo;
import com.borqs.music.tasks.GetSingerAreaTask;
import com.borqs.music.ui.adapter.OnlineSingerListAdapter;

public class OnlineSingerCategoryActivity extends OnlineBaseActivity {
    private final int LOAD_CONTENT_LIST_START = 1;
    private final int LOAD_CONTENT_LIST_COMPLETE = 2;
    private final int LOAD_CONTENT_LIST_FAILED = 3;

    private List<SingerInfo> SingerInfoList;
    private OnlineSingerListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SingerInfoList = SingerCache.get(this, CacheUtils.CACHE_TYPE_CATEGORY);

        mAdapter = new OnlineSingerListAdapter(this, SingerInfoList, getListView());
        setListAdapter(mAdapter);
        mHandler.sendEmptyMessage(LOAD_CONTENT_LIST_START);
    }

    private void executeTask() {
        GetSingerAreaTask task = new GetSingerAreaTask(singerAreaListener, mTicket);
        MusicExecutor excutor = MusicExecutor.getInstance();
        excutor.execute(task);
    }

    TaskAdapter singerAreaListener = new TaskAdapter() {

        public void onDone(Task task) {
            Message msg = mHandler.obtainMessage(LOAD_CONTENT_LIST_COMPLETE);
            msg.obj = task.getResult();
            mHandler.sendMessage(msg);
        };

        public void onFail(Task task) {
            mHandler.sendEmptyMessage(LOAD_CONTENT_LIST_FAILED);
        };
    };

    Handler mHandler = new Handler() {
        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LOAD_CONTENT_LIST_START:
                    mAdapter.setStatus(STATUS_LOADING);
                    mAdapter.notifyDataSetChanged();
                    executeTask();
                    break;
                case LOAD_CONTENT_LIST_COMPLETE:
                    List<SingerInfo> singers = (List<SingerInfo>) msg.obj;

                    Context context = OnlineSingerCategoryActivity.this;
                    SingerCache.put(context, CacheUtils.CACHE_TYPE_CATEGORY, singers);

                    SingerInfoList.clear();
                    SingerInfoList.addAll(singers);
                    mAdapter.setStatus(STATUS_NORMAL);
                    mAdapter.notifyDataSetChanged();
                    break;
                case LOAD_CONTENT_LIST_FAILED:
                    mAdapter.setStatus(STATUS_FAILED);
                    mAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        if (!checkActiveNetworkStatus()) {
            return;
        }

        int type = mAdapter.getItemViewType(position);
        if (type == TYPE_STATUS) {
            if (mAdapter.getStatus() != STATUS_LOADING) {
                mHandler.sendEmptyMessage(LOAD_CONTENT_LIST_START);
            }
            return;
        }

        if (type == TYPE_NORMAL) {
            Intent intent = new Intent();
            intent.setClass(this, OnlineSingerListActivity.class);
            intent.putExtra("area", (int) id);
            intent.putExtra("title", SingerInfoList.get(position).name);
            startActivity(intent);
        }
    }
}
